package com.luu.game.service.logic.combat;

import com.luu.game.constant.AtcConstant;
import com.luu.game.data.dto.CombotInfoDTO;
import com.luu.game.data.dto.ElfInfoDTO;
import com.luu.game.data.dto.SkillInfoDTO;
import com.luu.game.service.logic.elfPropertyFactory.BaseElfProperty;
import com.luu.game.service.logic.elfPropertyFactory.ElfPropertyFactory;
import org.springframework.stereotype.Component;

/**
 * 战斗计算类
 */
@Component
public class Combat {
    private Combat(){}

    /**
     * 攻击方法
     * 计算公式：技能伤害 * 属性克制 - 防御力抵抗 = 扣除hp数值
     * 技能伤害：攻击力 * 技能倍率
     * 防御力：防御力 * 25%
     * 属性克制：+ 25%
     *
     * @param atc 攻击方
     * @param defend 防守方
     * @return
     */
    public static CombotInfoDTO combatCountForAtc(ElfInfoDTO atc,ElfInfoDTO defend, SkillInfoDTO skillInfo){
        final CombotInfoDTO result = new CombotInfoDTO();
        boolean isOver = false;

        // 攻击方：技能伤害
        Double skillHarm = countSkillHarm(atc.getAtk() , skillInfo.getHurt());
        // 防守方：防御力
        Double defendNumber = countRatio(defend.getDefense());
        // 攻击方对防守方是否属性克制
        Double propertyNumber = isPropertyRestrain(atc,defend);
        // 防守方血量
        final Double defendHp = defend.getHp();
        Double hp = countHurtFormula(defendHp, skillHarm, propertyNumber, defendNumber);
        defend.setHp(hp);

        if (hp <= 0){
            isOver = true;
        }

        final Double giveHurt = giveHurt(skillHarm, propertyNumber, defendNumber);

        result.setDefendInfo(defend);
        result.setOver(isOver);
        result.setGiveHurt(giveHurt);
        return result;
    }

    /**
     * 计算剩余血量
     * @param defendHp 防守方血量
     * @param skillHarm 技能伤害
     * @param propertyNumber 技能倍率
     * @param defendRatio 防守方防御力
     * @return 防守方血量
     */
    private static Double countHurtFormula(Double defendHp, Double skillHarm, Double propertyNumber, Double defendRatio){
        final Double giveHurt = giveHurt(skillHarm, propertyNumber, defendRatio);
        return defendHp - giveHurt;
    }

    /**
     * 伤害公式
     * @param skillHarm 技能伤害
     * @param propertyNumber 技能倍率
     * @param defendRatio 防守方防御力
     * @return
     */
    private static Double giveHurt(Double skillHarm, Double propertyNumber, Double defendRatio){
        return skillHarm * propertyNumber + skillHarm - defendRatio;
    }

    /**
     * 计算伤害公式的防御力
     * @param defendRatio
     * @return
     */
    private static Double countRatio(Double defendRatio){
        return defendRatio * AtcConstant.DEFEND_RATIO;
    }

    /**
     * 计算伤害公式的技能伤害
     * @param atc
     * @param hurt
     * @return
     */
    private static Double countSkillHarm(Double atc, Double hurt){
        return atc * hurt;
    }

    /**
     * 是否属性克制
     * 火 -> 草 -> 水 -> 火
     * @param atc
     * @param defend
     * @return
     */
    public static Double isPropertyRestrain(ElfInfoDTO atc, ElfInfoDTO defend){
        final String atcProperty = atc.getProperty();
        final String defendProperty = defend.getProperty();

        final BaseElfProperty elfProperty = new ElfPropertyFactory(atcProperty).createElfProperty();

        if (elfProperty == null){
            return null;
        }

        final Double returnData = elfProperty.isPropertyRestrain(defendProperty);
        return returnData;

    }

    /**
     * 战斗结算方法
     * @param robot
     * @return
     */
    public Double combatCountForExp(ElfInfoDTO robot){
        return null;
    }



}
